//
//  LSLoginInputCell.m
//  LookScore
//
//  Created by wr on 2017/12/5.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLoginInputCell.h"
#import "LSAccountArchive.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "WXApi.h"
@interface LSLoginInputCell()<UITextFieldDelegate>
@property(copy,nonatomic)NSString *textString;

@end

@implementation LSLoginInputCell

+ (LSLoginInputCell *)loginInputTableViewCellByTableView:(UITableView *)tableView{
    
    LSLoginInputCell *cell= [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSLoginInputCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addContentView];
    }

    return self;
}

-(void)addContentView{
   
    UIImageView *inputImg=[[UIImageView alloc]init];
    [self.contentView addSubview:inputImg];
    self.inputImg=inputImg;
    inputImg.sd_layout.leftEqualToView(self.contentView).offset(LSSpaceLeftWidth2+10).centerYEqualToView(self.contentView).widthIs(15).heightIs(20);
    
    UITextField *inputTF=[[UITextField alloc]init];
    inputTF.font=LSFontSize13;
    inputTF.textColor=LSTitleGrayColor1;
    inputTF.delegate=self;
    inputTF.clearButtonMode=UITextFieldViewModeAlways;
    [self.contentView addSubview:inputTF];
    self.inputTF=inputTF;
    inputTF.sd_layout.leftSpaceToView(inputImg, 10).centerYEqualToView(self.contentView).rightEqualToView(self.contentView).offset(-LSSpaceLeftWidth2).heightIs(LSControlHeight);
    
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=LSLineGrayColor;
    [self.contentView addSubview:lineView];
    self.lineView=lineView;
    lineView.sd_layout.leftEqualToView(self.contentView).offset(LSSpaceLeftWidth2).bottomSpaceToView(self.contentView,0).rightEqualToView(self.contentView).offset(-LSSpaceLeftWidth2).heightIs(.5f);
    
    UIButton *eyesBtn=[[UIButton alloc]init];
    [eyesBtn setBackgroundImage:[UIImage imageNamed:@"LoginCloseEyes"] forState:UIControlStateNormal];
    [eyesBtn setBackgroundImage:[UIImage imageNamed:@"LoginOpenEyes"] forState:UIControlStateSelected];
    [eyesBtn addTarget:self action:@selector(clickEyesBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:eyesBtn];
    self.eysBtn=eyesBtn;
    eyesBtn.sd_layout.rightEqualToView(inputTF).offset(-35).centerYEqualToView(self.contentView).widthIs(20).heightIs(10);
    
}
#pragma mark 🔥 UIButtonTarget
- (void)clickEyesBtn:(UIButton *)sender{
    
    if (self.isEyesOpenedBlock) {
        self.isEyesOpenedBlock(sender);
    }
}
#pragma mark 🔥 UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    self.lineView.backgroundColor=LSNavMainColor;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (self.cellInputTextBlock) {
        self.cellInputTextBlock(self);
    }
    return YES;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


@implementation LSLoginEventCell


+ (LSLoginEventCell *)loginEventTableViewCellByTableView:(UITableView *)tableView{
    
    LSLoginEventCell *cell= [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSLoginEventCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addContentView];
    }
    
    return self;
}

-(void)addContentView{
    BOOL select = [[NSUserDefaults standardUserDefaults] boolForKey:@"select"];
    
    UIButton *remPassWordBtn=[UIButton new];
    remPassWordBtn.titleLabel.font=LSFontSize11;
    [remPassWordBtn setTitle:@"记住密码" forState:UIControlStateNormal];
    remPassWordBtn.selected = select;
    [remPassWordBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [remPassWordBtn setImage:[UIImage imageNamed:@"LoginNoAuto"] forState:UIControlStateNormal];
    [remPassWordBtn setImage:[UIImage imageNamed:@"LoginYesAuto"] forState:UIControlStateSelected];
    [remPassWordBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 3, 0, -3)];
    remPassWordBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [remPassWordBtn addTarget:self action:@selector(rememberPassWordEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:remPassWordBtn];
    self.remPassWordBtn=remPassWordBtn;
    remPassWordBtn.sd_layout.leftEqualToView(self.contentView).offset(LSSpaceLeftWidth2).topEqualToView(self.contentView).offset(10).heightIs(20).autoWidthRatio(6);
    
    UIButton *forgetKeyBu=[UIButton new];
    forgetKeyBu.titleLabel.font=LSFontSize11;
    [forgetKeyBu setTitle:@"忘记密码 ？" forState:UIControlStateNormal];
    [forgetKeyBu setTitleColor:LSSubColorf25e59 forState:UIControlStateNormal];
    forgetKeyBu.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    forgetKeyBu.tag=2;
     [forgetKeyBu addTarget:self action:@selector(LoginOrRegisterAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:forgetKeyBu];
    forgetKeyBu.sd_layout.topEqualToView(self.contentView).offset(10).rightEqualToView(self.contentView).offset(-LSSpaceLeftWidth2).heightIs(20).leftSpaceToView(remPassWordBtn, 20);
    
    UIButton *loginBu=[UIButton new];
    loginBu.titleLabel.font=LSFontSize15;
    [loginBu setTitle:@"登录" forState:UIControlStateNormal];
    loginBu.layer.cornerRadius=5;
    loginBu.layer.masksToBounds=YES;
    
    [loginBu setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    [loginBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateNormal];
    [loginBu setBackgroundImage:[CommonClass getImageWithBgColor:LSNavMainColor] forState:UIControlStateHighlighted];
    loginBu.tag=0;
    loginBu.contentVerticalAlignment=UIControlContentHorizontalAlignmentCenter;
    [loginBu addTarget:self action:@selector(LoginOrRegisterAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:loginBu];
   loginBu.sd_layout.leftEqualToView(self.contentView).offset(LSSpaceLeftWidth2).topSpaceToView(remPassWordBtn, 29).rightEqualToView(self.contentView).offset(-LSSpaceLeftWidth2).heightIs(LSButtonHeight);
    
    UIButton *registerBu=[UIButton new];
    registerBu.titleLabel.font=LSFontSize15;
    registerBu.layer.cornerRadius=5;
    registerBu.layer.masksToBounds=YES;
    registerBu.layer.borderColor=LSLineGrayColor.CGColor;
    registerBu.layer.borderWidth=1;
    registerBu.tag=1;
    [registerBu setTitle:@"注册" forState:UIControlStateNormal];
    [registerBu setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    registerBu.contentVerticalAlignment=UIControlContentHorizontalAlignmentCenter;
    [registerBu addTarget:self action:@selector(LoginOrRegisterAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:registerBu];
    
    registerBu.sd_layout.leftEqualToView(self.contentView).offset(LSSpaceLeftWidth2).topSpaceToView(loginBu, 13).rightEqualToView(self.contentView).offset(-LSSpaceLeftWidth2).heightIs(LSButtonHeight);
    
    
    UIView *thirdView=[[UIView alloc]init];
    [thirdView setBackgroundColor:[UIColor whiteColor]];
    [self.contentView addSubview:thirdView];//加入第三方登录的View
    thirdView.sd_layout.leftEqualToView(self.contentView).topSpaceToView(registerBu, 0).rightEqualToView(self.contentView).bottomEqualToView(self.contentView);
    
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=LSLineGrayColor;
    [thirdView addSubview:lineView];
    lineView.sd_layout.leftEqualToView(thirdView).offset(80).rightEqualToView(thirdView).offset(-80).centerYEqualToView(thirdView).offset(-50).heightIs(.5);
    
    
    UILabel *thirdLab=[[UILabel alloc]init];
    [thirdLab setText:@"使用第三方账号登录"];
    [thirdLab setTextColor:LSTitleGrayColor3];
    thirdLab.font=LSFontSize12;
    thirdLab.textAlignment=NSTextAlignmentCenter;
    [thirdLab setBackgroundColor:LSTitleWhiteColor];
    float textwidth=[CommonClass textWidthByString:thirdLab.text sizeFont:12 layoutWidth:self.contentView.width spaceWidth:10];
    [thirdView insertSubview:thirdLab aboveSubview:lineView];
    thirdLab.sd_layout.centerXEqualToView(thirdView).centerYEqualToView(thirdView).offset(-50).heightIs(20).widthIs(textwidth);
    
    //使用第三方QQ账号登录
    //WithFrame:CGRectMake((antherLoginView.width-qqBtnWidth)/2.0,CGRectGetMaxY(loginLable.frame)+25, qqBtnWidth, qqBtnHight)]
//    [WXApi registerApp:@"wx91600145b561a18e"];//注册微信第三方登录
    
    
    BOOL isQQInstalled = [TencentOAuth iphoneQQInstalled];//[TencentOAuth iphoneQQInstalled];如何QQ登录改成有的话才显示没有的话 隐藏的话 苹果会拒绝 应该QQ在没有按钮的情况下  照样可以去网页版登录
    BOOL isWXInstalled = [WXApi isWXAppInstalled];
    if (isQQInstalled==NO&&isWXInstalled==NO) {
        thirdView.hidden=YES;
    }else{
        
        if (isQQInstalled) {
            UIButton *qqMyButton=[[UIButton alloc]init];
            [qqMyButton setImage:[UIImage imageNamed:@"LoginQQ"] forState:UIControlStateNormal];
            qqMyButton.tag=3;
            [qqMyButton setTitle:@"QQ" forState:UIControlStateNormal];
            [qqMyButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
            [qqMyButton setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
            [qqMyButton setImageEdgeInsets:UIEdgeInsetsMake(-10, 0, 10, 0)];
            [qqMyButton setTitleEdgeInsets:UIEdgeInsetsMake(25, -43, -15, 0)];
            [qqMyButton addTarget:self action:@selector(LoginOrRegisterAction:) forControlEvents:UIControlEventTouchUpInside];
            [thirdView addSubview:qqMyButton];
            if (isWXInstalled==NO) {
                 qqMyButton.sd_layout.centerXEqualToView(thirdView).topSpaceToView(thirdLab,30).heightIs(43).widthIs(43);
               
            }else{
             qqMyButton.sd_layout.centerXEqualToView(thirdView).offset(-40).topSpaceToView(thirdLab,30).heightIs(43).widthIs(43);
            }
            
        }
        
        if (isWXInstalled) {
            UIButton *weiXinMyButton=[[UIButton alloc]init];
            [weiXinMyButton setImage:[UIImage imageNamed:@"LoginWX"] forState:UIControlStateNormal];
            weiXinMyButton.tag=4;
            [weiXinMyButton setTitle:@"微信" forState:UIControlStateNormal];
            [weiXinMyButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
            [weiXinMyButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [weiXinMyButton setImageEdgeInsets:UIEdgeInsetsMake(-10, 0, 10, 0)];
            [weiXinMyButton setTitleEdgeInsets:UIEdgeInsetsMake(25, -43, -15, 0)];
            [weiXinMyButton addTarget:self action:@selector(LoginOrRegisterAction:) forControlEvents:UIControlEventTouchUpInside];
            [thirdView addSubview:weiXinMyButton];
            if (isQQInstalled==NO) {
               weiXinMyButton.sd_layout.centerXEqualToView(thirdView).topSpaceToView(thirdLab,30).heightIs(43).widthIs(43);
            }else{
                  weiXinMyButton.sd_layout.centerXEqualToView(thirdView).offset(40).topSpaceToView(thirdLab, 30).heightIs(43).widthIs(43);
               
            }
            

        }
        
       
    }
    
    
}

#pragma mark 🔥 LoginOrRegisterAction

-(void)LoginOrRegisterAction:(UIButton *)sender{
    
    if (self.LoginOrRegisterOrForgetKeyBlock) {
        self.LoginOrRegisterOrForgetKeyBlock(sender.tag);
    }
}

-(void)rememberPassWordEvent:(UIButton *)sender{
    sender.selected=!sender.selected;
    
    if (self.clickRememberKeyButtonBlock) {
        self.clickRememberKeyButtonBlock(sender.selected);
    }
}
-(void)createCAGradientLayerLinViewInView:(UIView *)destView{
    //  创建 CAGradientLayer 对象
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    
    //  设置 gradientLayer 的 Frame
    gradientLayer.frame = destView.bounds;
    
    //  创建渐变色数组，需要转换为CGColor颜色
    gradientLayer.colors = @[(id)[UIColor groupTableViewBackgroundColor].CGColor,
                             (id)[UIColor grayColor].CGColor,
                             (id)[UIColor groupTableViewBackgroundColor].CGColor];
    
    // 设置三种颜色变化点，取值范围 0.0~1.0
    gradientLayer.locations = @[@(0.25f) ,@(0.5f),@(0.75f)];
    
    // 设置渐变颜色方向，左上点为(0,0), 右下点为(1,1)
    gradientLayer.startPoint = CGPointMake(0.5, 0);
    gradientLayer.endPoint = CGPointMake(0.5, 1);
    
    //  添加渐变色到创建的 UIView 上去
    [destView.layer addSublayer:gradientLayer];
}


@end

